
import {ø, keep} from "../util/objects.js";
import * as utc from "../util/utc.js";
import * as clock from "../util/clock.js";
import {scalarProduct} from "./scalarProduct.js";
import * as sources from "./sources.js";
import {tr} from "../ux/translations.js";
import {newProduct} from "./productUtils.js";
import {createUnitDescriptors} from "../util/units.js";
import {gaiaPath} from "./gaia.js";
import {createSSTPalette} from "../palette/sst.js";
import {createSSTAPalette} from "../palette/ssta.js";


let palettes = {};

export function rtgValidTime(time_cursor) {
    let parts;
    if (time_cursor === "current") {
        // Each day, SST data for the previous day is made available. So "current" means yesterday.
        const now = keep(utc.parts(clock.now()), "year", "month", "day");
        parts = utc.add(now, {day: -1});
    } else {
        parts = ø(time_cursor);
    }
    return parts;
}

export function rtgPath(attr, name) {
    const isCurrent = attr.time_cursor === "current";
    const dir = isCurrent ? "current" : utc.print(attr.time_cursor, "{YYYY}/{MM}");
    const timestamp = isCurrent ? "current" : utc.print(attr.time_cursor, "{YYYY}{MM}{DD}");
    const file = `${timestamp}-${name}-rtg-0.5.epak`;
    return gaiaPath("data/rtgsst", dir, file);
}

function newRTGProduct(attr) {
    const validTime = rtgValidTime(attr.time_cursor);
    const stepper = step => function() {
        return rtgValidTime(utc.add(this.validTime(), step));
    };
    return {
        sourceHTML: sources.rtgsst,
        validTime: () => ø(validTime),
        prev2: stepper({day: -5}),
        prev1: stepper({day: -1}),
        next1: stepper({day: +1}),
        next2: stepper({day: +5}),
        navLabels: {
            prev2: `-5 ${tr`days`}`,
            prev1: `-1 ${tr`day`}`,
            next1: `+1 ${tr`day`}`,
            next2: `+5 ${tr`days`}`,
        },
    };
}

export function createRTGSST(attr) {
    return ø(newProduct(), newRTGProduct(attr), {
        type: "sea_surface_temp",
        descriptionHTML: {
            name: tr`Sea Surface Temperature`,
            qualifier: "",
        },
        paths: [rtgPath(attr, "sea_surface_temp")],
        builder: file => scalarProduct(file, /Temperature_surface_sparse/, {hasMissing: true}),
        unitDescriptors: createUnitDescriptors({
            "°C": {convert: x => x - 273.15,       precision: 1},
            "°F": {convert: x => x * 9/5 - 459.67, precision: 1},
            "K":  {convert: x => x,                precision: 1},
        }),
        scale: palettes.sst ??= createSSTPalette(),
        alpha: {single: 200, animated: 132},
    });
}

export function createRTGSSTA(attr) {
    return ø(newProduct(), newRTGProduct(attr), {
        type: "sea_surface_temp_anomaly",
        descriptionHTML: {
            name: tr`Sea Surface Temperature Anomaly`,
            qualifier: "",
        },
        paths: [rtgPath(attr, "sea_surface_temp_anomaly")],
        builder: file => scalarProduct(file, /Temperature_surface_anomaly_sparse/, {hasMissing: true}),
        unitDescriptors: createUnitDescriptors({
            "°C": {convert: x => x,       precision: 1},
            "°F": {convert: x => x * 9/5, precision: 1},
            "K":  {convert: x => x,       precision: 1},
        }),
        scale: palettes.ssta ??= createSSTAPalette(),
    });
}
